# 使用多阶段构建
# 第一阶段：构建前端
FROM node:18-alpine AS frontend-builder

WORKDIR /app/frontend
COPY flux_web/package*.json ./
RUN npm install
COPY flux_web .
RUN npm run build

# 第二阶段：构建后端
FROM maven:3.9.6-amazoncorretto-17 AS backend-builder

WORKDIR /app
COPY . .
# 复制Maven配置文件
COPY settings-aliyun.xml /root/.m2/settings.xml
RUN mvn -s /root/.m2/settings.xml clean package -DskipTests

# 第三阶段：运行应用
FROM amazoncorretto:17-alpine

WORKDIR /app

# 安装nginx、时区数据和其他必要工具
RUN apk add --no-cache nginx netcat-openbsd tzdata

# 设置时区为北京时间
ENV TZ=Asia/Shanghai

# 创建日志目录
RUN mkdir -p /app/logs && chmod 777 /app/logs

# 复制前端构建产物到nginx目录
COPY --from=frontend-builder /app/frontend/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf

# 复制各个模块的jar包
COPY --from=backend-builder /app/btm-chat/target/*.jar /app/btm-chat.jar
COPY --from=backend-builder /app/mcp-server-mysql/target/*.jar /app/mcp-server-mysql.jar
COPY --from=backend-builder /app/mcp-server-business/target/*.jar /app/mcp-server-business.jar
COPY --from=backend-builder /app/extract-doris-info/target/*.jar /app/extract-doris-info.jar
COPY --from=backend-builder /app/mcp-server-organization/target/*.jar /app/mcp-server-organization.jar

# 设置环境变量
ENV JAVA_OPTS="-Xmx512m"

# 复制启动脚本
COPY docker-entrypoint.sh /app/
RUN chmod +x /app/docker-entrypoint.sh

# 暴露对外服务端口
# 8889: 前端服务
# 8822: btm-chat API服务
# 8833: doris-info API服务
EXPOSE 8889 8822 8833

ENTRYPOINT ["sh", "/app/docker-entrypoint.sh"]